//
//  GPTestView.m
//  StudyDemo01
//
//  Created by 心晨 on 2024/7/22.
//

#import "GPTestView.h"
#import "AsyncDrawLabel.h"

@interface GPTestView()

@property (nonatomic, strong) AsyncDrawLabel *nameLabel;

@property (nonatomic, strong) UILabel *ageLabel;

@end

@implementation GPTestView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.nameLabel];
        [self.nameLabel.layer setNeedsDisplay];
        [self addSubview:self.ageLabel];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _ageLabel.frame = CGRectMake(20, 50, self.bounds.size.width - 50, 20);
        });
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.nameLabel];
        [self.nameLabel.layer setNeedsDisplay];
        [self addSubview:self.ageLabel];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _ageLabel.frame = CGRectMake(20, 50, self.bounds.size.width - 50, 20);
        });
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"drawRect drawRect");
}

- (void)layoutSubviews {
    NSLog(@"layoutSubviews layoutSubviews");
}

- (void)setNeedsLayout {
    NSLog(@"setNeedsLayout setNeedsLayout");
}

- (AsyncDrawLabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[AsyncDrawLabel alloc] initWithFrame:CGRectMake(20, 20, self.bounds.size.width - 40, 20)];
        _nameLabel.text = @"GePing";
        _nameLabel.font = [UIFont systemFontOfSize:16];
    }
    return _nameLabel;
}

- (UILabel *)ageLabel {
    if (!_ageLabel) {
        _ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.bounds.size.width - 40, 20)];
        _ageLabel.text = @"25";
        _ageLabel.font = [UIFont systemFontOfSize:16];
    }
    return _ageLabel;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    NSLog(@"hitTest %@", view);
    return view;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesBegan touchesBegan");
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
